//
//  MineViewModel.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/10/29.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "MineViewModel.h"
#import "UserInfoRequest.h"
#import "MineCountModel.h"

@interface MineViewModel ()
@property(nonatomic, strong) MineCountModel *countModel;
@end

@implementation MineViewModel
- (void)request:(BOOL)willAppear {
    if (willAppear && self.loaded == NO) {
        self.loaded = YES;
        return;
    }
    if (willAppear) {
        !self.refreshUserInfo ?: self.refreshUserInfo();
    }
    [self.parentVc openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
        if (isLogin == NO) {
            return;
        }
        [UserInfoRequest request_userInfoComption:^(BOOL success, UserInfoModel * _Nonnull model) {
            !self.refreshUserInfo ?: self.refreshUserInfo();
        }];
        [self request_mineCount];
    }];
}
- (void)request_mineCount {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_MineCount];
    WK(weakSelf)
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:parameters FinishBlock:^(id  _Nonnull data) {
        if (![data isKindOfClass:[NSDictionary class]]) {
            return;
        }
        NSInteger code = [[data valueForKey:@"code"] integerValue];
        if (code != 10000) {
            return;
        }
        NSDictionary *resultDic = [data valueForKey:@"data"];
        weakSelf.countModel = [MineCountModel mj_objectWithKeyValues:resultDic];
        !weakSelf.refreshCount ?: weakSelf.refreshCount(weakSelf.countModel);
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
}
@end
